<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 头部操作栏 -->
            <div class="layui-form toolbar">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <button id="robotAgvInfoBtnAdd" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>
                    </div>
                </div>
            </div>
            <!-- 表格 -->
            <table class="layui-table" id="robotAgvInfoTable" lay-filter="robotAgvInfoTable"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="robotAgvInfoTableBar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<!-- 表单弹窗 -->
<script type="text/html" id="robotAgvInfoForm">
    <form lay-filter="robotAgvInfoForm" class="layui-form model-form">
        <input name="id" type="hidden"/>
        <div class="layui-form-item">
            <label class="layui-form-label">所属agv</label>
            <div class="layui-input-block">
                <select name="agvId" id="agvId" lay-filter="agvId" lay-verify="required" required></select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">位置信息，关节1的角度值</label>
            <div class="layui-input-block">
                <input name="positionOne" placeholder="请输入位置信息，关节1的角度值" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">位置信息，关节2的角度值</label>
            <div class="layui-input-block">
                <input name="positionTwo" placeholder="请输入位置信息，关节2的角度值" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">位置信息，关节3的角度值</label>
            <div class="layui-input-block">
                <input name="positionThree" placeholder="请输入位置信息，关节3的角度值" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">位置信息，关节4的角度值</label>
            <div class="layui-input-block">
                <input name="positionFour" placeholder="请输入位置信息，关节4的角度值" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">位置信息，关节5的角度值</label>
            <div class="layui-input-block">
                <input name="positionFive" placeholder="请输入位置信息，关节5的角度值" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">位置信息，关节6的角度值</label>
            <div class="layui-input-block">
                <input name="positionSix" placeholder="请输入位置信息，关节6的角度值" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">是否为机械臂初始位置</label>
            <div class="layui-input-block">
                <select name="init" lay-filter="init">
                    <option value="1">否</option>
                    <option value="0">是</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            <button class="layui-btn" lay-filter="robotAgvInfoFormSubmit" lay-submit>保存</button>
        </div>
    </form>
</script>

<!-- js部分 -->
<script>
    layui.use(['layer', 'form', 'table', 'admin'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var admin = layui.admin;
        var upload = layui.upload;

        // 渲染表格
        var insTb = table.render({
            elem: '#robotAgvInfoTable',
            url: config.base + 'enterprise/robot/admin/robotAgvInfo/list',
            page: true,
            cellMinWidth: 100,
            cols: [[
                {field: 'number', title: '序号',type: 'numbers' }
                ,{field: 'agvId', sort: true, title: '所属agv',templet:function(d){
                        let map = '';
                        admin.reqSync('enterprise/robot/admin/robotAgv/list', {id: d.agvId}, function (res) {
                            if (200 == res.code&&res.data.length!=0) {
                                map =  res.data[0].name;
                            }
                        }, 'GET', true);
                        return map;
                    }
                }
                ,{field: 'positionOne', sort: true, title: '位置信息，关节1的角度值'}
                ,{field: 'positionTwo', sort: true, title: '位置信息，关节2的角度值'}
                ,{field: 'positionThree', sort: true, title: '位置信息，关节3的角度值'}
                ,{field: 'positionFour', sort: true, title: '位置信息，关节4的角度值'}
                ,{field: 'positionFive', sort: true, title: '位置信息，关节5的角度值'}
                ,{field: 'positionSix', sort: true, title: '位置信息，关节6的角度值'}
                ,{field: 'statusDesc', sort: true, title: '状态'}
                ,{field: 'creatorName', sort: true, title: '添加人'}
                ,{field: 'createTime', sort: true, title: '添加时间'}
                ,{align: 'center', toolbar: '#robotAgvInfoTableBar', title: '操作', width: 200}
            ]],
            done: function (res, curr, count) {
            }
        });

        // 工具条点击事件
        table.on('tool(robotAgvInfoTable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') { //修改
                showEditModel(data);
            } else if (obj.event === 'del') { // 删除
                doDelete(obj);
            }
        });

        //监听排序
        table.on('sort(robotAgvInfoTable)', function(obj){
            table.reload('robotAgvInfoTable', {
                initSort: obj
                ,where: {
                    sort: obj.field
                    ,order: obj.type
                }
            });
        });

        // 添加按钮点击事件
        $('#robotAgvInfoBtnAdd').click(function () {
            showEditModel();
        });

        // 显示编辑弹窗
        function showEditModel(data) {
            admin.open({
                type: 1,
                area: '740px',
                offset: '65px',
                title: data ? '修改机械臂协作机器人本身仓储位置信息' : '添加机械臂协作机器人本身仓储位置信息',
                content: $('#robotAgvInfoForm').html(),
                success: function () {
                    if(data){
                        admin.reqSync('enterprise/robot/admin/robotAgv/list', {limit: 9999}, function (res) {
                            if (200 == res.code) {
                                loadSelectHtml(res.data, "请选择AGV", "agvId", 0);
                            }else {
                                layer.msg('获取AGV失败', {icon: 2, time: 1000});
                            }
                        }, 'GET', true);
                    }
                    if (!data){
                        admin.req('enterprise/robot/admin/robotAgv/list', {limit: 9999}, function (res) {
                            if (200 == res.code) {
                                loadSelectHtml(res.data, "请选择AGV", "agvId", 0);
                            }else {
                                layer.msg('获取AGV失败', {icon: 2, time: 1000});
                            }
                        }, 'GET', true);
                    }
                    if (data){
                        var option = $("form select[name='status']").find("option");
                        layui.each(option, function(index, item){
                            if(data.status  == item.text){
                                data.status = item.value;
                            }
                        });
                    }

                    form.val('robotAgvInfoForm', data);
                    // 表单提交事件
                    form.on('submit(robotAgvInfoFormSubmit)', function (d) {
                        layer.load(2);
                        admin.req(data ? 'enterprise/robot/admin/robotAgvInfo/update' : 'enterprise/robot/admin/robotAgvInfo/add', d.field, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.msg(res.msg, {icon: 1});
                                table.reload('robotAgvInfoTable');
                                layer.closeAll('page');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, data ? 'PUT' : 'POST');
                        return false;
                    });
                }
            });
        }

        // 删除
        function doDelete(obj) {
            layer.confirm('确定要删除吗？', {
                offset: '65px',
                skin: 'layui-layer-admin'
            }, function (i) {
                layer.close(i);
                layer.load(2);
                admin.req('enterprise/robot/admin/robotAgvInfo/' + obj.data.id, {}, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 200) {
                        layer.msg(res.msg, {icon: 1});
                        obj.del();
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'DELETE');
            });
        }
        //option渲染
        function loadSelectHtml(data, tips, htmltarget, type) {
            let html = ' <option value>' + tips + '</option>';
            if (!data || data == null) {
                return;
            }
            for (let i = 0; i < data.length; i++) {
                if (type === 0) {
                    html += `<option value='${data[i].id}'>${data[i].name}</option>`;
                } else if (type === 1) {
                    html += `<option value='${data[i].id}'>${data[i].corporateName}</option>`;
                }
            }

            $("form select[name='" + htmltarget + "']").html(html);
            form.render('select');
        }
    });
</script>